<?php

namespace app\api\controller;

use app\admin\model\Level;
use app\common\controller\Api;
use app\common\model\Notice;
use app\common\model\Room;
use think\Request;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $param    = $this->request->param();
        $userInfo = $this->auth->getUserinfo();
        unset($userInfo['userId']);
        unset($userInfo['openId']);
        unset($userInfo['token']);
        $nocite   = Notice::order('createtime desc')->field('id,title')->find();
        $roomList = Room::whereIn('is_over', [1,3])->order('createtime desc')
                        ->field('id,number,name,image,total_num totalNum,onlien_num onlineNum')
                        ->paginate($param['pageCount'], false, ['page' => $param['page']])
                        ->each(function ($item) {
                            $item['image'] = getCurrentUrl($item['image']);
                        });
        $userInfo['levelName'] = Level::where('id',$userInfo['level'])->value('name') ?? "";
        if($userInfo['gender'] == 1) $userInfo['gender'] = "男";
        if($userInfo['gender'] == 2) $userInfo['gender'] = "女";
        $data = [
            'userInfo' => $userInfo,
            'notice'   => $nocite,
            'list'     => $roomList
        ];
        $this->success('首页',$data);
    }

    /** 公告列表 */
    public function list()
    {
        $param = $this->request->param();
        $this->validateCommon('Index.list', $this->serial_number);
        $item = Notice::order('createtime desc')
                      ->paginate($param['pageCount'], false, ['page' => $param['page']])
                      ->each(function ($item) {
                          $item['image'] = getCurrentUrl($item['image']);
                      });
        $this->success('公告列表', $item);
    }

    /** 文本管理 */
    public function text()
    {
        $param = $this->request->param();
        $this->validateCommon('Index.text', $this->serial_number);
        $text = "";
        if($param['type'] == 1) $text = config('site.Privacy');
        if($param['type'] == 2) $text = config('site.about_us');
        $this->success('文本管理',$text);
    }
}
